<?php
namespace Home\Controller;
/**
 * 用户委托买卖
 * @author Administrator
 *
 */
class UserEntrustController extends HomeBaseController
{
    public function index(){
        $list = D('UserEntrust')->getList(array('uid' => session('uid')));
        $count = D('UserEntrust')->getCount(array('uid' => session('uid')));
        $this->assign('list', $list);
        $this->assign('count', $count);
        $this->display("userEntrust/index");
    }
    
    //取消订单
    public function cancelTradeAgent(){
        $resp['status']=0;
        $resp['msg']="撤单失败！";
        if(null == I("post.id")){
            $resp['msg']="请求参数为空！";
        }else{
            $cond['id'] = I("post.id");
            $result=M('user_entrust')->where($cond)->select();
            if(null == $result){
                $resp['msg']="记录不存在！";
            }else{
                //卖出域名
                if($result[0]['trans_type']==2){
                    $updcond="update dd_user_domains set trans_status=1 where uid=".session('uid')." and domain_typeid=".$result[0]['domain_typeid']." and check_status=3 and trans_status=2 and is_mortage=".$result[0]['trans_mode']." order by createdate limit ".$result[0]['surplus'];
                    $Upao = M();
                    $upnum = $Upao->execute($updcond);
                    M('CredFlow')->where('is_entrust='.$result[0]['id'])->setField(array('is_entrust'=>0));
                }  else {
                    //全款购买
                    if($result[0]['trans_mode']==1){
                        $dongjie=($result[0]['surplus'])*($result[0]['unit_price']);
                        M('UserAssets')->where('uid='.$result[0]['uid'])->setDec('locked_money',$dongjie);
                        M('UserAssets')->where('uid='.$result[0]['uid'])->setInc('notpresent_money',$dongjie);
                    }
                }
                $result = M('user_entrust')->where($cond)->setField('status',2);
                if($result !== false){
                    $resp['status']=1;
                    $resp['msg']="撤单成功！";
                };
            }
        };
        $this->ajaxReturn($resp,'JSON');
    }
}